<?php
namespace app\api\controller;

use app\api\logic\Index as IndexLogic;
use app\api\logic\User as UserLogic;
use app\api\logic\Area as AreaLogic;
use app\api\model\Merchant as MerchantModel;
use think\App;
use think\Controller;
use think\facade\Cache;

class Index extends Controller
{

    private $indexLogic;
    private $userLogic;
    private $areaLogic;

    public function __construct(App $app = null,IndexLogic $indexLogic,UserLogic $userLogic,AreaLogic $areaLogic)
    {
        parent::__construct($app);
        $this->indexLogic=$indexLogic;
        $this->userLogic=$userLogic;
        $this->areaLogic=$areaLogic;
    }

    /**
     * 拆分商户列表/帖子列表接口
     */
    public function merchantPosts(){
        return $this->merchant(1);
    }

    public function merchantMerchants(){
        return $this->merchant(2);
    }

    public function index()
    {
        echo 'index';
    }


    public function getAirport2(){
        $params['lon']  = $this->request->post('lon','');
        $params['lat']  = $this->request->post('lat','');
        $params['airport']  = $this->request->post('airport_id',0);

        $avatar=get_file_path(config('data.user_avatar_default')[0]);

        $country=config('data.index_default_country');
        $country_name=config('data.index_default_country_name');
        $abbreviation_name=config('data.index_default_country_abbreviation');
        if(empty($params['lon']) || empty($params['lat'])){
            return json(['msg'=>'Airport cannot be empty','code'=>ERRNO['AIRPORTERR'],'data'=>['country_id'=>$country,'country_name'=>$country_name]]);
        }
        $airport=$this->indexLogic->handle($params);
        if(empty($airport)){
            if ($params['airport']){
                $airport = $params['airport'];
            }else{
                $handler=Cache::store('redis')->handler();
                $data=$handler->georadius(config('redis.redis_key.city_location'),$params['lon'],$params['lat'],500,'km',['COUNT'=>1]);
                if($data){
                    $info=explode('_',$data[0]);
                    $city=end($info);
                    $info=$this->areaLogic->getFields(['id'=>$city],['parent_id']);
                    $country=$info['parent_id'];
                    $countryInfo=$this->areaLogic->getFields(['id'=>$country],['name','abbreviation']);
                    $country_name=$countryInfo['name'];
                    $abbreviation_name=$countryInfo['abbreviation'];
                }
                return json(['msg'=>'Airport cannot be empty','code'=>ERRNO['AIRPORTERR'],'data'=>['country_id'=>$country,'country_name'=>$country_name,'abbreviation'=>$abbreviation_name]]);
            }

        }

        //判断是否登录
        $login_info=is_login();
        if($login_info){
            if($login_info->type!=config('data.role_type.user')){
                return json(['msg'=>'role error','code'=>ERRNO['ROLEERR']]);
            }
            $uid=$login_info->id;
            $params['userID']=$uid;
            $user_info=$this->userLogic->userInfo($uid,['avatar']);
            if($user_info['avatar']){
                $avatar=get_file_path($user_info['avatar']);
            }

        }
        $res=$this->indexLogic->getAirportInfo($airport);
        $res['airport_id']=intval($airport);
        $res['avatar']=$avatar;
        return json(['data'=>$res,'code'=>ERRNO['OK'],'msg'=>'ok']);

    }

    public function getAirport(){
        $params['lon']  = $this->request->post('lon','');
        $params['lat']  = $this->request->post('lat','');
        $params['airport']  = $this->request->post('airport_id','');
        $avatar=get_file_path(config('data.merchant_avatar_default'));
        //判断是否登录
        $login_info=is_login();
        if(!$login_info){
            //未登录
            $country=config('data.index_default_country');
            $country_name=config('data.index_default_country_name');
            $abbreviation_name=config('data.index_default_country_abbreviation');
            if(empty($params['lon']) || empty($params['lat'])){
                return json(['msg'=>'Airport cannot be empty','code'=>ERRNO['AIRPORTERR'],'data'=>['country_id'=>$country,'country_name'=>$country_name]]);
            }
            $airport=$this->indexLogic->handle($params);
            if(empty($airport)){
                if($params['airport']){
                    $airport=$params['airport'];    //未登录且附近没有机场，如果有最后一次选择最后一次机场
                }else{
                    $handler=Cache::store('redis')->handler();
                    $data=$handler->georadius(config('redis.redis_key.city_location'),$params['lon'],$params['lat'],500,'km',['COUNT'=>1]);
                    if($data){
                        $info=explode('_',$data[0]);
                        $city=end($info);
                        $info=$this->areaLogic->getFields(['id'=>$city],['parent_id']);
                        $country=$info['parent_id'];
                        $countryInfo=$this->areaLogic->getFields(['id'=>$country],['name','abbreviation']);
                        $country_name=$countryInfo['name'];
                        $abbreviation_name=$countryInfo['abbreviation'];
                    }
                    return json(['msg'=>'Airport cannot be empty','code'=>ERRNO['AIRPORTERR'],'data'=>['country_id'=>$country,'country_name'=>$country_name,'abbreviation'=>$abbreviation_name]]);
                }
            }
        }else{
            //已登录
            if($login_info->type!=config('data.role_type.merchant')){
                return json(['msg'=>'role error','code'=>ERRNO['ROLEERR']]);
            }
            $merchantModel=new MerchantModel();
            $info=$merchantModel->getInfo(['id'=>$login_info->id],['airport','avatar','terminal']);
            $airport=$info['airport'];
            $params['userID']=$login_info->id;
            if($info['avatar']){
                $avatar=get_file_path($info['avatar']);
            }
        }
        $res=$this->indexLogic->getAirportInfo($airport);
        if($login_info){
            $merchantModel=new MerchantModel();
            $info=$merchantModel->getInfo(['id'=>$login_info->id],['terminal']);
            //terminal参数未传递，且已登录，取商户所选terminal
            if(empty($params['terminal'])){
                $cur_terminal=explode(',',$info['terminal']);
                $res['terminal']=$cur_terminal;
            }
        }

        $res['airport_id']=intval($airport);
        $res['avatar']=$avatar;
        return json(['data'=>$res,'code'=>ERRNO['OK'],'msg'=>'ok']);
    }

    private function merchant($scene)
    {
        $params['airport']     =$this->request->post('airport','');

        $params['category']     =$this->request->post('category','');
        $params['sub_category'] =$this->request->post('sub_category','');
        $params['keyword']      =$this->request->post('keyword','');
        $params['terminal']     =$this->request->post('terminal','');
        $params['deadline']     =$this->request->post('deadline','');

        $params['page']         =$this->request->post('page',1);
        $params['pageSize']     =$this->request->post('pageSize',20);
        $params['type']         =$this->request->post('type',1);
        $params['scene']        =$scene;

//        echo json_encode($params);die;
//        var_dump($params);die;

        $cur_terminal='';

        //判断是否登录
//        $login_info=is_login();
//        if($login_info){
//            $merchantModel=new MerchantModel();
//            $info=$merchantModel->getInfo(['id'=>$login_info->id],['terminal']);
//            //terminal参数未传递，且已登录，取商户所选terminal
//            if(empty($params['terminal'])){
//                $cur_terminal=explode(',',$info['terminal']);
//            }
//        }
        if(empty($params['airport'])){
            return json(['msg'=>'Airport cannot be empty','code'=>ERRNO['ROLEERR']]);
        }
        $params['userID']=0;
        $login_info=is_login();
        if($login_info){
            $params['userID']=$login_info->id;
        }
        if($params['scene']==1){
            //获取帖子列表
            $res=$this->indexLogic->posts($params);
//            if($cur_terminal) $res['terminal']=$cur_terminal;
        }else{
            //获取商户列表
            $res=$this->indexLogic->merchants($params);
        }
        return json(['data'=>$res,'code'=>ERRNO['OK'],'msg'=>'ok']);
    }

    public function userPosts(){
        return $this->user(1);
    }

    public function userMerchants(){
        return $this->user(2);
    }

    private function user($scene){
        $params['airport']     =$this->request->post('airport','');

        $params['category']     =$this->request->post('category','');
        $params['sub_category'] =$this->request->post('sub_category','');
        $params['keyword']      =$this->request->post('keyword','');
        $params['terminal']     =$this->request->post('terminal','');
        $params['deadline']     =trim($this->request->post('deadline',''));

        $params['page']         =$this->request->post('page',1);
        $params['pageSize']     =$this->request->post('pageSize',20);
        $params['scene']        =$scene;


        if(empty($params['airport'])){
            return json(['msg'=>'Airport cannot be empty','code'=>ERRNO['PARAMERR']]);
        }

        $params['userID']=0;
        $login_info=is_login();
        if($login_info){
            $params['userID']=$login_info->id;
        }

        if($params['scene']==1){
            //获取帖子列表
            $res=$this->indexLogic->posts($params);
        }else{
            //获取商户列表
            $res=$this->indexLogic->merchants($params);
        }
        return json(['data'=>$res,'code'=>ERRNO['OK'],'msg'=>'ok']);
    }

    public function completion(){
        $params['scene']    = $this->request->post('scene',2);
        $params['keyword'] = $this->request->post('keyword','');
        if(empty($params['keyword'])){
            return json(['msg'=>'keyword cannot be empty','code'=>ERRNO['PARAMERR']]);
        }

        $res=$this->indexLogic->completion($params);
        return json(['data'=>$res,'code'=>ERRNO['OK'],'msg'=>'ok']);
    }
}
